﻿package app.view.mediator
{	
    import app.facade.App; 
	import app.facade.EApp;
	import app.model.proxy.AssetProxy;
	import app.view.component.Datas;
	import app.view.component.moment.Module;
	import flash.events.Event;
	import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.Mediator;
	import org.osflash.thunderbolt.Logger;
    
    /**
     * @author hungconcon
     */
    public class DataMediator extends Mediator implements IMediator
    {
        public static const NAME:String = "DataMediator";	
		
        public function DataMediator( viewComponent:Object ){
            super( NAME, viewComponent );						
        }	 
		override public function listNotificationInterests():Array {
            return [
						App.LOADING_ASSET
                   ];
        }
        override public function handleNotification( note:INotification ):void {
			var name:String = note.getName();
			var value:Object = note.getBody();
			switch (name){
				case App.LOADING_ASSET: 
					var asset:AssetProxy = facade.retrieveProxy(AssetProxy.NAME) as AssetProxy;
						ui.setList(asset.list);
				break;
				default: break;
            }
        }
		override public function onRegister():void {
			super.onRegister();
			ui.addEventHandler(App.LOADING_BUFFERING, eventHandlers);
			ui.addEventHandler(App.ASSET_LOAD_DONE, eventHandlers);
		}            		
		private function eventHandlers( event:Event ):void {
			switch( event.type ) {
				case App.ASSET_LOAD_DONE:
					sendNotification(App.LOADING_DONE);
					sendNotification(App.ASSET_LOAD_DONE);
					sendNotification(App.PREPARE_DATA_UI);
					break;
				case App.LOADING_BUFFERING:
					sendNotification(App.LOADING_BUFFERING, event.currentTarget.value);
					break;
				default: break;
			}
		}
        protected function get ui():Datas{
            return viewComponent as Datas;
        }
    }
}